<%@ taglib prefix="page" uri="http://com.qipay/pagelib" %>
<%@ page language="java" import="com.qipay.admin.cp.vo.CpInfoDTO" contentType="text/html;charset=utf-8" %>
<%@ page import="com.qipay.admin.tags.PageModel" %>
<%@ page import="com.qipay.admin.utils.SiteHelper" %>
<%@ page import="com.qipay.baas.model.Cp" %>
<%@ page import="com.qipay.baas.model.CpAccount" %>
<%@ page import="com.qipay.baas.model.User" %>
<%@ page import="com.qipay.user.entity.UserLevel" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.List" %>
<%@ page import="com.qipay.baas.model.CpInfo" %>
<!doctype html>
<html>
<head>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <jsp:include page="head.jsp"></jsp:include>
    <link rel="stylesheet" href="<%= request.getAttribute("StaticHost") %>/css/dataTables.bootstrap.min.css">
    <link rel="stylesheet" href="<%= request.getAttribute("StaticHost") %>/css/list.css">
    <script type="text/javascript">
        //document.getElementsByTagName("html")[0].style.width=screen.availWidth/100+"px";
        document.getElementsByTagName("html")[0].style.fontSize=screen.availWidth/200+"px";
    </script>
    <style type="text/css">
        .border{
            border:1px solid red;
        }
    </style>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
    <jsp:include page="nav.jsp"></jsp:include>
    <jsp:include page="left.jsp"></jsp:include>
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Main content -->
        <% CpInfo cpInfo = (CpInfo) request.getAttribute("detail");%>
        <section class="content">
            <div class="row">
                <div class="col-xs-12">
                    <div class="box">
                        <div class="box-header">
                            <h3 class="box-title"> 代理商【<%= cpInfo.getCpId()%>，<%= cpInfo.getName()%>】旗下的商户列表</h3>
                        </div>
                        <!-- /.box-header -->
                        <div class="tableContent" style="font-size: 12px">
                            <table width="100%" cellpadding="0" cellspacing="0" border="1">
                                <thead>
                                <tr>
                                    <th width="12%">商户ID</th>
                                    <th width="18%">商户名称</th>
                                    <th width="8%">法人</th>
                                    <th width="8%">余额</th>
                                    <th width="8%">可提</th>
                                    <th width="8%">垫支</th>
                                    <th width="8%">不可提</th>
                                    <th width="8%">联系人</th>
                                    <th width="14%">联系人手机</th>
                                    <th width="8%">状态</th>
                                    <th width="8%">签约</th>
                                    <th width="20%">操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <% List<CpInfoDTO> articles = (List<CpInfoDTO>) request.getAttribute("lst");
                                    PageModel pager = (PageModel) request.getAttribute("page");
                                    User currentUser = (User) session.getAttribute("user");
                                    UserLevel userLevel = SiteHelper.getUserLevelByLevel(currentUser.getLevel());
                                    String query = pageContext.getRequest().getServletContext().getContextPath() + "/proxy/" +cpInfo.getCpId() +"/child";
                                    if (articles != null && articles.size() > 0) {
                                        Iterator<CpInfoDTO> iter = articles.iterator();
                                        CpInfoDTO user = null;
                                        CpAccount cpAccount = null;
                                        Cp cpModel = null;
                                        while (iter.hasNext()) {
                                            user = iter.next();
                                            cpAccount = user.getCpAccount() != null ? user.getCpAccount() : new CpAccount();
                                            cpModel = user.getCp() != null ? user.getCp() : new Cp();
                                %>
                                <tr id="user_<%=user.getCpId()%>">

                                    <td>
                                        <a href="${pageContext.request.contextPath}/cp/<%= user.getCpId()%>" style="color: #00a65a">
                                            <%= user.getCpId()%>
                                        </a>
                                    </td>
                                    <td>
                                        <a href="${pageContext.request.contextPath}/cp/<%= user.getCpId()%>" style="color: #00a65a">
                                            <%= user.getName()%>
                                        </a>
                                    </td>
                                    <td>
                                        <%= user.getLegalPerson()%>
                                    </td>
                                    <td>
                                        <%= SiteHelper.getMoneyFormatAll(cpAccount.getBalance()/100.0)%>
                                    </td>
                                    <td>
                                        <%= SiteHelper.getMoneyFormatAll(cpAccount.getBalanceAvailable()/100.0)%>
                                    </td>
                                    <td>
                                        <%= SiteHelper.getMoneyFormatAll(cpAccount.getBalanceAdvance()/100.0)%>
                                    </td>
                                    <td>
                                        <%= SiteHelper.getMoneyFormatAll(cpAccount.getBalanceDisabled()/100.0)%>
                                    </td>
                                    <td>
                                        <%= user.getContacts()%>
                                    </td>
                                    <td>
                                        <%= user.getContactsMobile()%>
                                    </td>
                                    <td id="trgoods<%= user.getCpId()%>"
                                        class="<%= cpModel.getAccountState()!=null&&cpModel.getAccountState()==2?"success":""%>">
                                        <%= SiteHelper.getAccountString(cpModel.getAccountState())%>
                                    </td>
                                    <td class="<%= cpModel.getContractState()!=null&&cpModel.getContractState()==1?"info":""%>">
                                        <%= SiteHelper.getContractString(cpModel.getContractState())%>
                                    </td>
                                    <td>
                                        <a href="${pageContext.request.contextPath}/cp/<%= user.getCpId()%>/channel">
                                            <button type="button" class="button back-green" style="font-size: 1rem">渠道</button></a>
                                        <% if (userLevel == UserLevel.ADMIN) {%>
                                        <a name="lnkDelete" s="<%= cpModel.getAccountState()%>" pk="<%= user.getCpId()%>" href="javascript:;">
                                            <button type="button" class="button back-green" style="font-size: 1rem"><%= cpModel.getAccountState() != null && cpModel.getAccountState() == 2 ? "禁用" : "启用"%></button>
                                        </a>
                                        <a href="${pageContext.request.contextPath}/cp/<%= user.getCpId()%>">
                                            <button type="button" class="button back-green" style="font-size: 1rem">修改</button></a>
                                        <a  name="lnkRemove" href="javascript:;" pk="<%= user.getCpId()%>">
                                            <button type="button" class="button back-green" style="font-size: 1rem">删除</button></a>
                                        <%}%>

                                    </td>
                                </tr>
                                <%
                                    }
                                } else {
                                %>
                                <tr>
                                    <td colspan="12">未找到数据</td>
                                </tr>
                                <% }%>
                                </tbody>
                            </table>
                            <div class="col-sm-10">

                                <div class="dataTables_paginate paging_simple_numbers" id="example1_paginate">
                                    <page:page totalSum="<%= pager.getTotal()%>"
                                               url="<%= query%>"
                                               pageNo="<%= pager.getPage()%>"
                                               pageSize="<%= pager.getPageSize()%>"></page:page>
                                </div>
                            </div>
                        </div>
                        <!-- /.box-body -->
                    </div>
                    <!-- /.box -->
                </div>
                <!-- /.col -->
            </div>
            <!-- /.row -->
        </section>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
    <jsp:include page="foot.jsp"></jsp:include>
</div>
<script src="<%= request.getAttribute("StaticHost") %>/js/jquery.dataTables.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/dataTables.bootstrap.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/jquery.slimscroll.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/fastclick.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/adminlte.min.js"></script>
<script>
    $(function () {
        $('#example2').DataTable({
            'paging': false,
            'lengthChange': false,
            'searching': false,
            'ordering': true,
            'info': true,
            'autoWidth': false
        });
        $("[name='lnkDelete']").click(function () {
            var that = $(this);
            var pk = $(this).attr("pk");
            var state = $(this).attr("s");
            var isSuccess = state == "2";
            if (confirm(isSuccess ? "确定禁用吗？" : "确定启用吗？")) {
                state = isSuccess ? 0 : 200;
                if (pk > 0) {
                    $.ajax({
                        url: "${pageContext.request.contextPath}/ajax?n=setcpaccountstate&id=" + pk + "&id2=" + state,
                        type: "GET",
                        async: true,//true异步 false同步
                        context: this,
                        timeout: 10000,
                        success: function (data) {
                            if (data != null && data == "1") {
                                if (isSuccess) {
                                    $("#trgoods" + pk).removeClass("success");
                                    $("#trgoods" + pk).html("禁用");
                                    $(this).html("启用");
                                    $(this).attr("s", "0");
                                }
                                else {
                                    $("#trgoods" + pk).addClass("success");
                                    $("#trgoods" + pk).html("启用");
                                    $(this).html("禁用");
                                    $(this).attr("s", "2");
                                }
                            }
                            else {
                                alert("fail:" + data);
                            }
                        },
                        error: function (d, s, b) {
                            alert("失败:" + b);
                        }
                    });
                }
            }
        });

        $("[name='lnkRemove']").click(function () {
            var pk = $(this).attr("pk");
            if (pk > 0) {
                if (confirm("确定删除吗？")) {
                    $.ajax({
                        url: "${pageContext.request.contextPath}/ajax?n=delcpbyid&id=" + pk,
                        type: "GET",
                        async: true,//true异步 false同步
                        context: this,
                        timeout: 10000,
                        success: function (data) {
                            if (data != null && data == "1") {
                                $("#user_" + pk).hide(100);
                            }
                            else {
                                alert("fail:" + data);
                            }
                        },
                        error: function (d, s, b) {
                            alert("失败:" + b);
                        }
                    });
                }
            }

        });
    })
</script>
</body>
</html>

